package com.lanou.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.mapper.OwnerUserMapper;
import com.lanou.pojo.OwnerUser;
import com.lanou.service.OwnerUserService;
import com.lanou.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName OwnerUserServiceImpl
 * @Description: TODO
 * @Author 郅占伟
 * @Date 2023年10月24日
 * @Version: 1.0
 */
@Service
public class OwnerUserServiceImpl implements OwnerUserService {

    @Autowired
    private OwnerUserMapper ownerUserMapper;

    @Override
    public String getOwnerList(String pageNum, String pageSize, String userName, String idCard, String phoneNumber) throws JsonProcessingException {
        System.out.println(pageNum+":"+pageSize);
        // 分页查询
        //处理分页查询
//        if (pageNum == null || "".equals(pageNum)) {
//            pageNum = "1";
//        }
//        if (pageSize == null || "".equals(pageSize)) {
//            pageSize = "5";
//        }
        PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        // 根据数据查询业主信息
        List<OwnerUser> ownerUsers = ownerUserMapper.selectOwnerByInfo(userName, idCard, phoneNumber);

        //封装数据
        PageInfo<OwnerUser> pageInfo = new PageInfo<>(ownerUsers);
        JsonResult<PageInfo<OwnerUser>> jsonResult = new JsonResult<>();
        jsonResult.setData(pageInfo);
        jsonResult.setStatus(200);
        //处理数据（转化json）
        return JSON.toJSONString(jsonResult);
    }

    @Override
    public String addModifyOwner(Map<String, String> map) {
        String companyName = map.get("companyName");
        String idCard = map.get("idCard");
        String ownerName = map.get("ownerName");
        String phoneNumber = map.get("phoneNumber");
        String remark = map.get("remark");
        String sex = map.get("sex");
        String status = map.get("status");
        String id = map.get("userid");

        if ("男".equals(sex)) {
            sex = "0";
        } else if ("女".equals(sex)) {
            sex = "1";
        } else {
            sex = "2";
        }

        // 不等于空字符串时更新
        if (!"".equals(id)) {
            ownerUserMapper.updateOwner(companyName, idCard, ownerName, phoneNumber, remark, sex, status, id);
            // 等于时是插入
        } else {
            ownerUserMapper.insertOwner(companyName, idCard, ownerName, phoneNumber, remark, sex, status);
        }

        JsonResult<String> jsonResult = new JsonResult<>();
        jsonResult.setStatus(200);
        //处理数据（转化json）
        return JSON.toJSONString(jsonResult);

    }
}
